import { mapState, mapMutations } from 'vuex';
import miniFront from "@/layouts/miniFront/miniFront";
import InsPortalDefApi from "../../api/portal/core/insPortalDef"
import OsUserApi from "@/api/user/org/osUser";
export default {
    mixins: [miniFront],
    computed: {
        ...mapState({
            navigation: state => state.appSetting.navigation,
            menus: state => state.appSetting.menus,
        })
    },
    methods: {
        ...mapMutations('appSetting', ['setAppKey', 'setOpenFront', 'setSelecteKeys', 'setActiveKey', 'setActiveFront', 'setFrontType', 'setNotHomeAppsByMenu']),
        i18n(name, text, key) {
            return this.$ti18(name, text, "applicationLang", key);
        },
        getFirstMenu(menus) {
            //获取第一个菜单，
            if (!menus[0].children || menus[0].children.length === 0) {
                return menus[0]
            }
            let getChildren = (data) => {
                for (let item of data) {
                    if (item.children && item.children.length > 0) {
                        return getChildren(item.children)
                    } else {
                        return item;
                    }
                }
            }
            return getChildren(menus);
        },
        newWindowDialog(currentApp) {
            //新窗口打开
            if (this.isHttpUrl(currentApp.path)) {
                window.open(currentApp.path, "_blank");
                return
            }
            let url = `${currentApp.path}/${currentApp.clientCode}`;
            window.open(url, "_blank");
        },
        //是否http://或者https://开头的外部url
        isHttpUrl(url) {
            return url.startsWith("http://") || url.startsWith("https://");
        },
        innerFront(currentApp) {//内置微前端
            let appMenu = this.menus.find(menu => menu.id === currentApp.pkId);
            //获取微前端第一个菜单和路由；
            let { firstPath, firstMenu } = this.openFirstFront(appMenu);
            currentApp['key'] = currentApp.clientCode;
            this.innnerFrontOpen(currentApp, firstPath, firstMenu, true);
        },
        innerMenu(currentApp) {//内置菜单
            let appMenu = this.menus.find(menu => menu.id === currentApp.pkId);
            //设置还原window的vue挂载
            if (window.Vue2) {
                window.Vue = window.Vue2;
                window.Vue2 = undefined;
            }
            //清除微前端选中；
            this.setActiveFront({})
            //设置是否打开微前端，这里全部给false,当进入微前端时，会在main.js中设置状态;
            this.setOpenFront(false);

            if (!appMenu || !appMenu.children || appMenu.children.length == 0) {
                this.$message.warning(this.i18n('tips.authorize', '请先为应用配置菜单，并授权！'));
                return;
            }
            this.setAppKey(appMenu.key);
            let child = this.getFirstMenu([appMenu]);
            let isSelect = this.navigation.admin.find(item => { return item.key == child.key });
            this.setActiveKey(child.key);
            if (!isSelect) {
                this.navigation.admin.push(child);
            }
            let pathParams = {
                name: child.key
            }
            if (child.appType == '1') {
                pathParams.query = { appId: child.appId };
            }
            this.setFrontType('user');
            //存储VUEX状态。
            localStorage.setItem("store", JSON.stringify(this.$store.state));
            let routeData = this.$router.resolve(pathParams);
            window.open(routeData.href, '_blank')
        },
        backToAdmin() {
            let url = process.env.VUE_APP_ADMIN_FRONT + "/design";
            window.open(url);

        },
        hasMenus(menus) {
            let ary = menus.filter(item => {
                return item.menuType == "C" && item.isApp == 'N'
            });
            return ary.length > 0;
        },
        async appPreview(currentApp) {
            if (currentApp.status === "0") {
                this.$message.warning(this.i18n('tips.stop', '应用已暂停，请先启用!'));
                return
            }
            let self_ = this;
            let menus = await OsUserApi.getMenusByAppId(currentApp.appId);
            let hasMenus = this.hasMenus(menus);
            if (!hasMenus) {
                this.$message.warning(this.i18n('tips.authorize', '请先为应用配置菜单，并授权！'));
                return;
            }
            let { path, clientCode, menuNavType, appId } = currentApp;
            if (menuNavType === 2) {
                window.open(`/main/microApp${path}/${clientCode}?appId=${appId}`, '_blank')
            } else if (menuNavType === 0) {
                let url = `/main/webApp/${clientCode}?appId=${appId}&open=admin`;
                window.open(url, '_blank')
            } else {
                let url = `${path}/${clientCode}`;
                window.open(url, "_blank");
            }
        },
    }
}